<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\UserCollect;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
//    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';
    protected $noNeedLogin = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }
    protected $userId = 1 ;
    public function info()
    {
        $user = \app\common\model\User::where('id', $this->userId)->find();
        $this->success('', [
            'user' => [
                'username' => $user->username,
                'img_url' => $user->img_url,
                'sur_money' => $user->sur_money,
                'auth_status' => $user->auth_status ,
                'nopass'
            ],
            'total' => [
                'num1' => 1,
                'num2' => 20,
            ],
            'tel' => 156261827618
        ]);
    }
    public function allinfo()
    {
        $user = \app\common\model\User::where('id', $this->userId)->find();
        $this->success('', [
            'user' => [
                'username' => $user->username,
                'img_url' => $user->img_url,
                'auth_status' => $user->auth_status ,
                'nopass' => $user['nopass'] ,
                'realname' => $user['realname'] ,
                'idcard' => $user['idcard'] ,
                'idcard_img_url' => $user['idcard_img_url'] ,
                'idcard_img_url2' => $user['idcard_img_url2'] ,
            ],

        ]);
    }

    public function collect()
    {
        $type = $this->request->post('type' , 0 , 'intval') ;
        $page = $this->request->post('page' , 1 , 'intval') ;
        $orderIds = UserCollect::where('user_id' , $this->userId)
            ->where('status' , 1)->column('order_id') ;
        $list = \app\common\model\Order::where('id' , 'in' , $orderIds)
            ->where('type' , $type)
            ->field('title,price,id,img_url')->page($page , 1)->select() ;
        $this->success('请求成功' , [
            'list' => $list
        ]);
    }
    public function collect_add()
    {
        $id = $this->request->post('id' , 0 , 'intval') ;
        $info = UserCollect::where('user_id' , $this->userId)->where('id' , $id)->find() ;
        if($info == null){
            UserCollect::create([
                'user_id' => $this->userId ,
                'order_id' => $id ,
            ]) ;
        }
        else{
            UserCollect::where('id' , $id)->update([
                'status' => $info['status'] == 1 ? -1 : 1
            ]) ;
        } ;
        $this->success('操作成功' , [

        ]);
    }


    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @ApiParams (name="account", type="string", required=true, description="账号")
     * @ApiParams (name="password", type="string", required=true, description="密码")
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    public function attestation()
    {
        $img1 = $this->request->post('img1' , '' , 'trim') ;
        $img2 = $this->request->post('img2' , '' , 'trim') ;
        $username = $this->request->post('username' , '' , 'trim') ;
        $idcard = $this->request->post('idcard' , '' , 'trim') ;
        if($img1 == '')
        {
            $this->error('请上传正面身份证');
        }
        if($img2 == '')
        {
            $this->error('请上传反面身份证');
        }
        if($username == '')
        {
            $this->error('请输入真实姓名');
        }
        if($idcard == '')
        {
            $this->error('请输入身份证号');
        }
        $info = \app\common\model\User::where('id', $this->userId)->find() ;
        if($info['auth_status'] != 0 || $info['auth_status'] != 2)
        {
            $this->error('当前审核状态不正确，不可提交');
        }

        \app\common\model\User::where('id', $this->userId)->update([
            'idcard_img_url' => $img1 ,
            'idcard_img_url2' => $img2 ,
            'realname' => $username ,
            'idcard' => $idcard ,
            'auth_status' => 1 ,
        ]) ;
        $this->success('提交成功，后台审核中..');

    }


}
